package com.example.k.list;

import android.app.ListFragment;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.os.Handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 佳星 on 08-02-0002.
 */
public class KListFragment extends ListFragment {
    private WifiInfo wifiInfo;
    private WifiManager wifiManager;
    private List<ScanResult> mWifiList;
    private boolean stopFlag = false;
    private Thread thread;
    private ArrayList<Wifidat> arrayList = ArrDat.getArr();
    private WifiInfoAdapter listAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listAdapter = new WifiInfoAdapter(arrayList);
        setListAdapter(listAdapter);

        wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        wifiInfo    = wifiManager.getConnectionInfo();
        wifiManager.startScan();
        mWifiList   =wifiManager.getScanResults();


        thread = new Thread(){
            @Override
            public void run() {
                int a = 0;
                HashMap<String,Integer> mac_leve       = new HashMap<String,Integer>() ;       //mac和信号强度
                HashMap<String,Integer> mac_time = new HashMap<String,Integer>() ;//mac和对应出现次数
                HashMap<String,String>  map_name  = new HashMap<String,String>() ;//mac和ssid

                while(!stopFlag){
                    wifiManager.startScan();
                    mWifiList=wifiManager.getScanResults();

                    for (ScanResult aMWifiList : mWifiList) {
                        String bssid = aMWifiList.BSSID;
                        String name  = aMWifiList.SSID;
                        int level    = aMWifiList.level;
                        int n = 0;//信号强度和
                        int m = 0;//出现次数和
                        if (mac_leve.get(bssid) != null) {
                            n = mac_leve.get(bssid);
                            m = mac_time.get(bssid);
                        }
                        map_name.put(bssid, name);
                        mac_time.put(bssid, m + 1);
                        mac_leve.put(bssid, n + level);
                    }
                    ++a;
                    if (a%10 == 0){
                        arrayList.clear();
                        for(Object o:mac_leve.keySet()){
                            int n = mac_leve.get(o);
                            int m = mac_time.get(o);

                            String _ssid = map_name.get(o);
                            String _mac  = (String)o;
                            String _lev  = n/m+"";
                            String _time = mac_time.get(o)+"";
                            Wifidat dat  = new Wifidat(_mac,_ssid,_lev,_time);

                            arrayList.add(dat);
                            hand.post(r);
                        }
                        mac_leve.clear();
                        mac_time.clear();
                        a = 0;
                    }
                    try {
                        sleep(100);

                    } catch(Exception e){
                        e.printStackTrace();
                    }

                }
            }
        };
    }
    Handler hand = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() {
            listAdapter.notifyDataSetChanged();
        }
    };
    class WifiInfoAdapter extends ArrayAdapter<Wifidat>{
        public WifiInfoAdapter(ArrayList<Wifidat> obj) {
            super(getActivity(), 0, obj);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.line,null);
            }

            TextView mac = (TextView) convertView.findViewById(R.id.mac);
            TextView lev = (TextView) convertView.findViewById(R.id.lev);
            TextView ssid = (TextView) convertView.findViewById(R.id.ssid);
            TextView time = (TextView) convertView.findViewById(R.id.time);

            Wifidat dat = getItem(position);
            mac.setText(dat.getMac());
            lev.setText(dat.getLev());
            ssid.setText(dat.getSsid());
            time.setText(dat.getTime());

            return convertView;
        }
    }

    @Override
    public void onPause() {
        stopFlag = true;
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        stopFlag = false;
        thread.start();
    }
}
